<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

class UpOld
{
    protected $order;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);

        // 必须是报单区
        if ($is_bao) {
            $this->upMyself();
            $this->upChain();
        }

        // 新报单
        $is_new = ProductArea::checkNew1($product_id);
        if ($is_new) {
            $this->upNew();
            $this->upNewReferrer();
        }
    }

    // 升级自己
    protected function upMyself()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;
        // var_dump($amount);exit;

        $set = $this->set;
        $user = $this->user;

        if ($amount >= $set->svip2) {
            if ($user->level < UserLevel::$keys['svip2']) {
                $user->level = UserLevel::$keys['svip2'];
                $user->save();
            }
        }

        if ($amount >= $set->svip1) {
            if ($user->level < UserLevel::$keys['svip1']) {
                $user->level = UserLevel::$keys['svip1'];
                $user->save();
            }
        }

        // if ($amount >= $set->vip2) {
        //     if ($user->level < UserLevel::$keys['vip2']) {
        //         $user->level = UserLevel::$keys['vip2'];
        //         $user->save();
        //     }
        // }

        if ($amount >= $set->vip1) {
            if ($user->level < UserLevel::$keys['vip1']) {
                $user->level = UserLevel::$keys['vip1'];
                $user->save();
            }
        }

    }

    // 升级关系链
    protected function upChain()
    {
        $referrer = Chain::getReferrer($this->order->user_id, true);

        if ($referrer && $referrer->level >= UserLevel::$keys['vip1']) {

            $children = Chain::getChildrenLevel($referrer->id, [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                UserLevel::$keys['svip1'],
                UserLevel::$keys['svip2'],
            ]);

            $count = $this->set->vip2;

            if (count($children) >= $count) {

                if ($referrer->level < UserLevel::$keys['vip2']) {
                    $referrer->level = UserLevel::$keys['vip2'];
                    $referrer->save();
                }

            }

        }
    }

    private function upNew()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        $user = $this->user;
        $set = [
            'vip1' => 2999,
            'vip2' => 9800,
            'vip3' => 29990,
            'vip4' => 98000,
        ];

        if ($amount >= $set['vip4']) {
            if ($user->new_level < NewLevel::$keys['vip4']) {
                $user->new_level = NewLevel::$keys['vip4'];
                $user->save();
            }
        }
        if ($amount >= $set['vip3']) {
            if ($user->new_level < NewLevel::$keys['vip3']) {
                $user->new_level = NewLevel::$keys['vip3'];
                $user->save();
            }
        }
        if ($amount >= $set['vip2']) {
            if ($user->new_level < NewLevel::$keys['vip2']) {
                $user->new_level = NewLevel::$keys['vip2'];
                $user->save();
            }
        }
        if ($amount >= $set['vip1']) {
            if ($user->new_level < NewLevel::$keys['vip1']) {
                $user->new_level = NewLevel::$keys['vip1'];
                $user->save();
            }
        }
    }

    private function upNewReferrer()
    {
        $referrer = Chain::getReferrer($this->order->user_id, true);
        if (!$referrer) {
            return;
        }



        $has_vip1 = $this->getChildrenByLevelCount($referrer->id, NewLevel::$keys['vip1']);
        $has_vip2 = $this->getChildrenByLevelCount($referrer->id, NewLevel::$keys['vip2']);
        $has_vip3 = $this->getChildrenByLevelCount($referrer->id, NewLevel::$keys['vip3']);

        // var_dump($has_vip1, $has_vip2, $has_vip3);exit;

        if ($has_vip3 >= 10) {
            if ($referrer->new_level < NewLevel::$keys['vip4']) {
                $referrer->new_level = NewLevel::$keys['vip4'];
                $referrer->save();
            }
        }
        if ($has_vip2 >= 10) {
            if ($referrer->new_level < NewLevel::$keys['vip3']) {
                $referrer->new_level = NewLevel::$keys['vip3'];
                $referrer->save();
            }
        }
        if ($has_vip1 >= 10) {
            if ($referrer->new_level < NewLevel::$keys['vip2']) {
                $referrer->new_level = NewLevel::$keys['vip2'];
                $referrer->save();
            }
        }
    }

    // 新等级
    private function getChildrenByLevelCount($user_id, $level)
    {
        $count = User::find()
            ->where(['referrer_id' => $user_id])
            ->andWhere(['new_level' => $level])
            ->count();

        return intval($count);
    }
}